﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Thunder
{
    class Game

    {
        private const int GameWidth = 1000;
        private const int GameHeight = 700;

        /// <summary>
        /// 时间
        /// </summary>
        private System.Timers.Timer Timer { get; set; }

        //定义委托
        public delegate void Gamechanged();
        //定义事件
        public event Gamechanged GameChanged;
        //定义友方飞机
        private plane Plane;
        //定义友方子弹
        private List<Bullet> bullets { get; set; }
        //定义敌方飞机
        private List<enemy_plane> enemy_Planes { get; set; }
        private List<BOSS> BOSSes { get; set; }
        //定义时间
        private DateTime nextshoot { get; set; }
        private DateTime nextenemy { get; set; }
        private DateTime nextboss { get; set; }
        //定义游戏状态
        bool Gamenow = true;


        public Game()
        {
            //初始化友方子弹
            bullets = new List<Bullet>();
            //初始化敌方飞机
            enemy_Planes = new List<enemy_plane>();
            BOSSes = new List<BOSS>();
            //初始化友方飞机
            Plane = new plane();
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;

            Timer.Start();
        }


        internal void Draw(object graphics, Size size)
        {
            throw new NotImplementedException();
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟处理友方子弹
            if (System.DateTime.Now > nextshoot)
            {
                //子弹位置确定
                Rectangle MyLocation = Plane.Rec;
                Bullet bullet = new Bullet(new Rectangle(MyLocation.X + 45, MyLocation.Y, 30, 40));
                bullets.Add(bullet);
                nextshoot = System.DateTime.Now.AddSeconds(0.5);
            }

            //子弹发射
            Bullet bulletnow = null;
            foreach (var bullet in bullets)
            {
                bullet.Move();
                //子弹是否出界
                if (bullet.Rectangle.Y <0)
                {
                    bulletnow =bullet ;
                    break;
                }              
                GameChanged();
            }
            //移除出界子弹
            if (bulletnow != null)
                bullets.Remove(bulletnow);

            //敌机产生
            if (System.DateTime.Now > nextenemy)
            {
                //小飞机
                Random random = new Random();
                enemy_plane enemy = new enemy_plane(new Rectangle(random.Next(100, 800), 0, 100, 100));

                enemy_Planes.Add(enemy);
                nextenemy = System.DateTime.Now.AddSeconds(5);
            }
            
            if (System.DateTime.Now>nextboss)
            {
                //BOSS
                Random random = new Random();
                BOSS boss = new BOSS(new Rectangle(random.Next(100, 800), 0, 200, 200));
                BOSSes.Add(boss);
                nextboss = System.DateTime.Now.AddSeconds(15);
            }

            //敌机移动
            enemy_plane enemynow = null;
            BOSS Bossnow = null;
            foreach (var enemy_plane in enemy_Planes)
            {
                enemy_plane.Move();
                if (enemy_plane.Rectangle.Y>630)
                {
                    enemynow = enemy_plane;
                    break;
                }
                GameChanged();
            }
            foreach (var BOSS in BOSSes)
            {
                BOSS.Move();
                if (BOSS.Rectangle.Y>630)
                {
                    Bossnow = BOSS;
                    break;
                }
                GameChanged();
            }
            //敌机出界游戏结束
            if (enemynow!=null)
            {
                Gamenow = false;
            }
            if (Bossnow!=null)
            {
                Gamenow = false;
            }
            HandleTouch();
            GameChanged();

        }
        /// <summary>
        /// 处理碰撞
        /// </summary>
        private void HandleTouch()
        {
            
            Bullet bulletnow = null;
            enemy_plane enemynow = null;
            for (int j = 0; j < enemy_Planes.Count; j++)
            {
                for (int k = 0; k < bullets.Count; k++)
                {
                    for (int i = 0; i < BOSSes.Count; i++)
                    {
                        if (BOSSes[i].Rectangle.IntersectsWith(bullets[k].Rectangle))
                        {
                            //击中Boss
                            bulletnow = bullets[k];//记下来
                            //BOSS血量-1
                            BOSSes[i].Bosslife--;   
                        }
                        //友军撞上BOSS
                        if (BOSSes[i].Rectangle.IntersectsWith(Plane.Rec))
                        {
                            Gamenow = false;
                        }
                    }
                    if (enemy_Planes[j].Rectangle.IntersectsWith(bullets[k].Rectangle))
                    {
                        //击中敌军
                        bulletnow = bullets[k];//记下来
                        enemynow = enemy_Planes[j];//记下来
                    }
                }
                // 友军撞上飞机
                if (enemy_Planes[j].Rectangle.IntersectsWith(Plane.Rec))
                    {
                    Gamenow = false;
                    }
                
                
            }
            
            //移除子弹
            if (bulletnow != null)
            {
                bullets.Remove(bulletnow);
            }
            //移除敌军
            if (enemynow != null)
            {
                enemy_Planes.Remove(enemynow);
            }
            //移除所有血量为0的BOSS
            BOSSes.RemoveAll(o => { return o.Bosslife <= 0; });


        }
        /// <summary>
        /// 绘制场景
        /// </summary>
        /// <param name="g">绘图句</param>
        /// <param name="size">尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            ///定义空白图    
            Image img = new Bitmap(GameWidth, GameHeight);
            Graphics _g = Graphics.FromImage(img);
            ///定义矩形
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            _g.DrawImage(Properties.Resources.bg2, rec);

            Plane.Draw(_g);
            //绘制子弹
            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Draw(_g);
            //绘制敌军
            for (int i = 0; i < enemy_Planes.Count; i++)
                enemy_Planes[i].Draw(_g);
            for (int i = 0; i < BOSSes.Count; i++)
                BOSSes[i].Draw(_g);
            
            //游戏失败
            if (Gamenow == false)
            {
                _g.DrawImage(Properties.Resources.GameOver, rec);
            }



            //todo

            g.DrawImage(img, new Rectangle(new Point(0, 0), size));


        }




        /// <summary>
        /// 键盘指令输入
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(string key)
        {
            Plane.KeyDown(key);
        }
    }
}
    

